package com.atguigu.java;

import org.junit.Test;

import java.io.*;

/**
 * 处理流之一：缓冲流的使用
 * <p>
 * <p>
 * 1.缓冲流
 * BufferInputStream
 * BufferOutputStream
 * BufferReader
 * BufferWriter
 * <p>
 * <p>
 * 2.作用：
 * 提高流的读取、写入速度
 *
 * @author shkstart
 * @create 2021-01-27 17:43
 */
public class BufferedTest {
    @Test
    public void test1() {
        long start = System.currentTimeMillis();
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            //1.创建FIle对象
            File file = new File("Capture001.png");
            File file1 = new File("Capture003.png");
            //2。造流
            //2.1 造节点流
            FileInputStream fileInputStream = new FileInputStream(file);
            FileOutputStream fileOutputStream = new FileOutputStream(file1);
            //2.2  造缓冲流
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            //3.输入输出操作
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bufferedInputStream.read(bytes)) != -1) {
                bufferedOutputStream.write(bytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            //要求必须先关外层的流 再关内层的流
            try {
                assert bufferedOutputStream != null;
                bufferedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //说明:再关闭外层流的同时，内层流也会自动关闭。关于内层流的关闭我们可以省略
//        fileOutputStream.close();
//        fileInputStream.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("总时长：" + (end - start));

    }


}
